package cn.sirun.com.fca.account.presenter.impl;

import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.account.constant.AccountActionConst;
import cn.sirun.com.fca.account.loader.PasswordLoader;
import cn.sirun.com.fca.account.presenter.AccountFlowPresenter;
import cn.sirun.com.fca.account.presenter.IPasswordPresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

public class PasswordPresenter extends AccountFlowPresenter<PasswordLoader> implements IPasswordPresenter {

    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    public void setPassword(String password) {
        getLoader().setPassword(password)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        PasswordPresenter.this.getActionListener().onAction(
                                AccountActionConst.Normal.ACTION_SET_PASSWORD_RESULT,
                                baseResponse);
                    }
                });
    }

    @Override
    public void setEmialPassword(String password) {
        getLoader().setEmailPassword(password)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        PasswordPresenter.this.getActionListener().onAction(
                                AccountActionConst.Normal.ACTION_SET_PASSWORD_RESULT,
                                baseResponse);
                    }
                });
    }

    @Override
    public void resetSetPassword(String password) {
        getLoader().resetSetPassword(password)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        PasswordPresenter.this.getActionListener().onAction(
                                AccountActionConst.Normal.ACTION_RESET_PASSWORD_RESULT,
                                baseResponse);
                    }
                });
    }

    @Override
    public void resetEmailSetPassword(String password) {
        getLoader().resetSetEmailPassword(password)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        PasswordPresenter.this.getActionListener().onAction(
                                AccountActionConst.Normal.ACTION_RESET_PASSWORD_RESULT,
                                baseResponse);
                    }
                });
    }

    @Override
    protected PasswordLoader createLoader() {
        return new PasswordLoader();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
